lots of clarifications and providing full api path

Judy Ngai 9 years ago
parent
commit
0ead3673b4
2 changed files with 20 additions and 35 deletions
  1. 7 25
      app/models/agents/aftership_agent.rb
  2. 13 10
      spec/models/agents/aftership_agent_spec.rb

+ 7 - 25
app/models/agents/aftership_agent.rb

@@ -10,21 +10,19 @@ module Agents
10 10
 
11 11
       To be able to use the Aftership API, you need to generate an `API Key`. You need a paying plan to use their tracking feature.
12 12
 
13
-      You can use this agent to retrieve tracking data. You have to provide a specific `path` request and its associated option.
13
+      You can use this agent to retrieve tracking data.
14 14
  
15
-      To get all trackings for your packages please enter `path` for key and `trackings` for the option.
16
-      To get tracking for a specific tracking number, add the extra keys `slug`, `tracking_number` and their associated values. Set `single_tracking_request` to true.
17
-
18
-      To get the last checkpoint of a package set key to `path` and option to `last_checkpoint`. Please provide `slug` and `tracking_number`. Set `last_checkpoint_request` to true.
19
-
20
-      `slug` is a unique courier code. 
15
+      Provide the `path` for the API endpoint that you'd like to hit. For example, for all active packages, enter `trackings` 
16
+      (see https://www.aftership.com/docs/api/4/trackings), for a specific package, use `trackings/SLUG/TRACKING_NUMBER` 
17
+      and replace `SLUG` with a courier code and `TRACKING_NUMBER` with the tracking number. You can request last checkpoint of a package 
18
+      by providing `last_checkpoint/SLUG/TRACKING_NUMBER` instead.
21 19
 
22 20
       You can get a list of courier information here `https://www.aftership.com/courier`
23 21
 
24 22
       Required Options:
25 23
 
26 24
       * `api_key` - YOUR_API_KEY.
27
-      * `path and its associated options`
25
+      * `path request and its full path`
28 26
     MD
29 27
 
30 28
     event_description <<-MD
@@ -92,14 +90,6 @@ module Agents
92 90
       }
93 91
     end
94 92
 
95
-    def single_tracking_request?
96
-      boolify(interpolated[:single_tracking_request])
97
-    end
98
-
99
-    def last_checkpoint?
100
-      boolify(interpolated[:last_checkpoint_request])
101
-    end
102
-
103 93
     def working?
104 94
       !recent_error_logs?
105 95
     end
@@ -110,11 +100,7 @@ module Agents
110 100
     end
111 101
 
112 102
     def check
113
-      if single_tracking_request? || last_checkpoint?
114
-        response = HTTParty.get(single_or_checkpoint_tracking_url, request_options)
115
-      else
116
-        response = HTTParty.get(event_url, request_options)
117
-      end
103
+      response = HTTParty.get(event_url, request_options)
118 104
       events = JSON.parse response.body
119 105
       create_event :payload => events
120 106
     end
@@ -128,10 +114,6 @@ module Agents
128 114
       base_url + "#{URI.encode(interpolated[:path].to_s)}"
129 115
     end
130 116
 
131
-    def single_or_checkpoint_tracking_url
132
-      base_url + "#{URI.encode(interpolated[:path].to_s)}/#{URI.encode(interpolated[:slug].to_s)}/#{URI.encode(interpolated[:tracking_number].to_s)}"
133
-    end
134
-
135 117
     def request_options
136 118
       {:headers => {"aftership-api-key" => interpolated['api_key'], "Content-Type"=>"application/json"} }
137 119
     end

+ 13 - 10
spec/models/agents/aftership_agent_spec.rb

@@ -9,11 +9,15 @@ describe Agents::AftershipAgent do
9 9
       :headers => {"Content-Type" => "text/json"}
10 10
     )
11 11
 
12
+    stub_request(:get, "trackings/usps/9361289878905919630610").to_return(
13
+      :body => File.read(Rails.root.join("spec/data_fixtures/aftership.json")),
14
+      :status => 200,
15
+      :headers => {"Content-Type" => "text/json"}
16
+    )
17
+
12 18
     @opts = {
13 19
       "api_key" => '800deeaf-e285-9d62-bc90-j999c1973cc9',
14
-      "path" => 'trackings',
15
-      "slug" => 'usps',
16
-      "tracking_number" => "9361289684090010005054"
20
+      "path" => 'trackings'
17 21
     }
18 22
 
19 23
     @checker = Agents::AftershipAgent.new(:name => "tectonic", :options => @opts)
@@ -30,15 +34,14 @@ describe Agents::AftershipAgent do
30 34
       expect(@checker.send(:event_url)).to eq("https://api.aftership.com/v4/trackings")
31 35
     end
32 36
 
33
-    it "should generate the correct single tracking url" do
34
-      @checker.options['single_tracking_request'] = true
35
-      expect(@checker.send(:single_or_checkpoint_tracking_url)).to eq("https://api.aftership.com/v4/trackings/usps/9361289684090010005054")
37
+    it "should generate the correct specific tracking url" do
38
+      @checker.options['path'] = "trackings/usps/9361289878905919630610"
39
+      expect(@checker.send(:event_url)).to eq("https://api.aftership.com/v4/trackings/usps/9361289878905919630610")
36 40
     end
37 41
 
38
-    it "should generate the correct checkpoint tracking url" do
39
-      @checker.options['path'] = 'last_checkpoint'
40
-      @checker.options['last_checkpoint_request'] = true
41
-      expect(@checker.send(:single_or_checkpoint_tracking_url)).to eq("https://api.aftership.com/v4/last_checkpoint/usps/9361289684090010005054")
42
+    it "should generate the correct last checkpoint url" do
43
+      @checker.options['path'] = "last_checkpoint/usps/9361289878905919630610"
44
+      expect(@checker.send(:event_url)).to eq("https://api.aftership.com/v4/last_checkpoint/usps/9361289878905919630610")
42 45
     end
43 46
   end
44 47